/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2011 All Rights Reserved.
 */
package com.liwei.nio.mina;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

/**
*
* @author lidian
* @version $Id: MyProtocalClient2.java, v 0.1 2011-1-27 上午10:14:08 lidian Exp $
*/
public class MyProtocalClient2 {
    private static final String HOST    = "192.168.10.8";
    private static final int    PORT    = 2500;
    static long                 counter = 0;
    final static int            FC1     = 100;
    static long                 start   = 0;

    /** 
     * 使用Mina的框架结构进行测试 
     *  
     * @param args 
     */
    public static void main(String[] args) throws IOException {
        start = System.currentTimeMillis();
        IoConnector connector = new NioSocketConnector();
        connector.getFilterChain().addLast("codec",
            new ProtocolCodecFilter(new MyProtocalCodecFactory(Charset.forName("GBK"))));
        connector.setHandler(new TimeClientHandler2());
        connector.getSessionConfig().setReadBufferSize(100);
        connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
        ConnectFuture connFuture = connector.connect(new InetSocketAddress(HOST, PORT));
        connFuture.addListener(new IoFutureListener<ConnectFuture>() {
            public void operationComplete(ConnectFuture future) {
                try {
                    if (future.isConnected()) {
                        IoSession session = future.getSession();
                        sendData(session);
                    } else {
                        System.out.println("连接不存在 ");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println("start client ...");
    }

    public static void sendData(IoSession session) throws IOException {
        for (int i = 0; i < FC1; i++) {
            String content = "afdjkdafk张新波测试" + i;
            MyProtocalPack pack = new MyProtocalPack((byte) i, content);
            session.write(pack);
            System.out.println("send data:" + pack);
        }
    }
}

class TimeClientHandler2 extends IoHandlerAdapter {
    @Override
    public void sessionOpened(IoSession session) {
        // Set reader idle time to 10 seconds.  
        // sessionIdle(...) method will be invoked when no data is read  
        // for 10 seconds.  
        session.getConfig().setIdleTime(IdleStatus.READER_IDLE, 60);
    }

    @Override
    public void sessionClosed(IoSession session) {
        // Print out total number of bytes read from the remote peer.  
        System.err.println("Total " + session.getReadBytes() + " byte(s)");
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) {
        // Close the connection if reader is idle.  
        if (status == IdleStatus.READER_IDLE) {
            session.close(true);
        }
    }

    @Override
    public void messageReceived(IoSession session, Object message) {
        MyProtocalPack pack = (MyProtocalPack) message;
        System.out.println("rec:" + pack);
    }
}
